package com.ruoyi.framework.config;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

@Component
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletResponse res = (HttpServletResponse) response;
        //    HttpServletRequest req = (HttpServletRequest) request;

        //    Enumeration<String> headerNames = req.getHeaderNames();
        //    while (headerNames.hasMoreElements()) {
        //      String k = headerNames.nextElement();
        //      System.out.println(k + " -> " + req.getHeader(k));
        //    }
        //    String appkey = req.getHeader("appkey");
        //    String apptoken = req.getHeader("Apptoken");
        //    if (apptoken == null) {
        //      apptoken = req.getHeader("Apptoken");
        //    }

        //        res.addHeader("Access-Control-Allow-Credentials", "true");
        res.addHeader("Access-Control-Allow-Origin", "*");
        res.addHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT");
        res.addHeader(
                "Access-Control-Allow-Headers",
                "Content-Type,X-CAF-Authorization-Token,sessionToken,satoken,token,Authorization,Apptoken,apptoken,X-Requested-With");
        if (((HttpServletRequest) request).getMethod().equals("OPTIONS")) {
            response.getWriter().println("ok");
            return;
        }

        //    if (appkey == null || appkey.isEmpty()) {
        //      res.setStatus(401);
        //      res.setCharacterEncoding("UTF-8");
        //      res.setContentType("application/json; charset=utf-8");
        //      res.getWriter().print("{\"code\": 401, msg\": \"no login\"}");
        //      return;
        //    }
        //    if (apptoken == null || apptoken.isEmpty()) {
        //      res.setStatus(401);
        //      res.setCharacterEncoding("UTF-8");
        //      res.setContentType("application/json; charset=utf-8");
        //      res.getWriter().print("{\"code\": 401, msg\": \"no login\"}");
        //      return;
        //    }

        //    List<DgAppkey> appkeyList = Const.appkeyList;
        //    Optional<DgAppkey> first =
        //        appkeyList.stream()
        //            .filter(
        //                x ->
        //                    //                    x.getAppkey().equalsIgnoreCase(appkey)
        //                    //                        &&
        //                    x.getApptoken().equalsIgnoreCase(apptoken))
        //            .findFirst();
        //    if (!first.isPresent()) {
        //      res.setStatus(401);
        //      res.setCharacterEncoding("UTF-8");
        //      res.setContentType("application/json; charset=utf-8");
        //      res.getWriter().print("{\"code\": 401, msg\": \"no login\"}");
        //      return;
        //    }

        chain.doFilter(request, response);

        //        boolean login = StpUtil.isLogin();
        //        if(login){
        //
        //            chain.doFilter(request, response);
        //        }else{
        //
        //            res.setStatus(401);
        //            res.setCharacterEncoding("UTF-8");
        //            res.setContentType("application/json; charset=utf-8");
        //
        //            res.getWriter().print("{\"code\": 401, msg\": \"no login\"}");
        //            return;
        //
        //        }

    }

    //    @Override
    //    public void destroy() {
    //
    //    }
}
